﻿Public Class FrmSuaHoSoDichVu
    Private Sub BatChucNang()
        txtTenCongTrinh.Enabled = True
        dtNgayNhan.Enabled = True
        txtHoSoNam.Enabled = True
        txtThanhPhanHoSo.Enabled = True
        cmbHuyen.Enabled = True
        cmbXa.Enabled = True
        cmbPhong.Enabled = True
        cmbKe.Enabled = True
        cmbKho.Enabled = True
        cmbTinhTrang.Enabled = True
        btnXoa.Enabled = True
        LamTuoi()
        txtTenCongTrinh.Focus()
    End Sub
    Private Sub TatChucNang()
        txtTenCongTrinh.Enabled = False
        dtNgayNhan.Enabled = False
        txtHoSoNam.Enabled = False
        txtThanhPhanHoSo.Enabled = False
        cmbHuyen.Enabled = False
        cmbXa.Enabled = False
        cmbPhong.Enabled = False
        cmbKe.Enabled = False
        cmbKho.Enabled = False
        cmbTinhTrang.Enabled = False
        btnXoa.Enabled = False
        LamTuoi()
    End Sub
    Private Sub LamTuoi()
        txtSoHoSo.Text = ""
        txtTenCongTrinh.Text = ""
        dtNgayNhan.Text = ""
        txtHoSoNam.Text = ""
        txtThanhPhanHoSo.Text = ""
    End Sub
    Private Function TimCongTrinh() As DataTable
        Dim sql As String
        sql = "Select * From CONGTRINH Where TenCongTrinh like '" & txtTenCongTrinh.Text & "'"
        Return LoadThongTin(sql)
    End Function
    Private Sub NapDSCongTrinh()
        Dim sql As String
        sql = "SELECT CONGTRINH.SoHoSo, CONGTRINH.TenCongTrinh, CONGTRINH.NgayNhan, CONVERT(char(10), CONGTRINH.NgayNhan, 103) AS NgayNhan1, CONGTRINH.HoSoNam, CONGTRINH.ThanhPhanHoSo, " &
              "TENHUYEN.T_Huyen, TENXA.TenXa, DMPhong.TenPhong, DMKe.TenKe, DMKho.TenKho, DMTinhTrang.TenTinhTrang " &
              "FROM CONGTRINH LEFT OUTER JOIN " &
              "DMPhong ON congtrinh.MaPhongID = DMPhong.MaPhongID LEFT OUTER JOIN " &
              "DMKe ON CONGTRINH.MaKeID = DMKe.MaKeID LEFT OUTER JOIN " &
              "DMKho ON CONGTRINH.MaKhoID = DMKho.maKhoID LEFT OUTER JOIN " &
              "DMTinhTrang ON CONGTRINH.MaTinhTrangID = DMTinhTrang.MaTinhTrangID LEFT OUTER JOIN " &
              "TENXA ON CONGTRINH.MaXa = TENXA.MaXa LEFT OUTER JOIN " &
              "TENHUYEN ON TENHUYEN.maH = TENXA.MaHuyen " &
              "WHERE CONGTRINH.TENCONGTRINH like N'%" & txtTenCongTrinh.Text.Trim() & "%'"
        'Kiem tra Ten cong trinh co nhap ko.
        dgvDSCongTrinh.DataSource = LoadThongTin(sql)
        dgvDSCongTrinh.Refresh()
    End Sub
   
    Private Sub FrmSuaHoSoDichVu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadHuyen(cmbHuyen)
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
        LoadPhong(cmbPhong)
        LoadKe(cmbKe)
        LoadKho(cmbKho)
        LoadTinhTrang(cmbTinhTrang)

        dtNgayNhan.Text = Date.Today()

        'TatChucNang()
        btnTim.Focus()
    End Sub

    Private Sub cmbHuyen_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbHuyen.SelectedIndexChanged
        LoadXa(Convert.ToInt32(cmbHuyen.SelectedValue.ToString), cmbXa)
    End Sub

   

    Private Sub btnSua_Click(sender As Object, e As EventArgs) Handles btnSua.Click
        Dim sql As String


        'Kiem tra Ten cong trinh khong duoc rong
        If (txtTenCongTrinh.Text.Trim = "") Then
            MessageBox.Show("Tên Công trình không được rỗng ")
            txtTenCongTrinh.Focus()
            Return
        End If

        'Kiem tra Ho so nam khong duoc rong
        If (txtHoSoNam.Text.Trim = "") Then
            MessageBox.Show("Hồ sơ năm không được rỗng ")
            txtHoSoNam.Focus()
            Return
        End If
        'Kiem tra Thanh phan ho so khong duoc rong
        If (txtThanhPhanHoSo.Text.Trim = "") Then
            MessageBox.Show("Thành phần hồ sơ không được rỗng ")
            txtThanhPhanHoSo.Focus()
            Return
        End If
        'Kiem tra nam phai nho hon nam hien tai
        If (Integer.Parse(txtHoSoNam.Text) > Date.Today.Year) Then
            MessageBox.Show("Năm phải nhỏ hơn năm " & Date.Today.Year.ToString)
            Return
        End If

        Dim NgayNhan As String
        NgayNhan = String.Format("{0:MM/dd/yyyy}", dtNgayNhan.Value)
        sql = "UPDATE CONGTRINH " &
              "SET TenCongTrinh = N'" & txtTenCongTrinh.Text.Trim & "' " &
              "   ,NgayNhan = '" & NgayNhan & "' " &
              "   ,HoSoNam  = " & txtHoSoNam.Text & "" &
              "   ,ThanhPhanHoSo = N'" & txtThanhPhanHoSo.Text.Trim & "'" &
              "   ,MaXa = '" & cmbXa.SelectedValue.ToString() & "'" &
              "   ,MaPhongID = '" & cmbPhong.SelectedValue.ToString() & "'" &
              "   ,MaKeID =  '" & cmbKe.SelectedValue.ToString & "'" &
              "   ,MaKhoID = '" & cmbKho.SelectedValue.ToString & "'" &
              "   ,MaTinhTrangID = '" & cmbTinhTrang.SelectedValue.ToString & "' " &
              "WHERE SoHoSo = '" & txtSoHoSo.Text & "'"

        Dim flag As Boolean
        flag = ThaoTacDuLieu(sql)

        If (flag = True) Then
            MessageBox.Show("Sửa thông tin thành công")
            LuuThongTinSua()
            LamTuoi()
            NapDSCongTrinh()
        End If

    End Sub

    Private Sub LuuThongTinSua()
        Dim sql As String
        sql = "INSERT INTO QL_SuaCongTrinh (SoHoSo,NguoiSua) VALUES ('" & txtSoHoSo.Text & "', '" & ModuleTenDangNhap.m_tennhap & "')"
        Dim t As Boolean
        t = ThaoTacDuLieu(sql)
    End Sub
    Private Sub LuuThongTinXoa()
        Dim sql As String
        sql = "INSERT INTO QL_XoaCongTrinh (SoHoSo,NguoiXoa) VALUES ('" & txtSoHoSo.Text & "', '" & ModuleTenDangNhap.m_tennhap & "')"
        Dim t As Boolean
        t = ThaoTacDuLieu(sql)
    End Sub

    Private Sub btnThoat_Click(sender As Object, e As EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub txtHoSoNam_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtHoSoNam.KeyPress

        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            MessageBox.Show("Kí tự không được chấp nhận. Chỉ chấp nhận số")
            e.Handled = True
        End If
    End Sub

   

    Private Sub FrmNhapHoSoDichVu_SizeChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnTim_Click(sender As Object, e As EventArgs) Handles btnTim.Click
        NapDSCongTrinh()
    End Sub

    Private Sub dgvDSCongTrinh_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSCongTrinh.CellClick
        Dim dong As Integer
        dong = e.RowIndex

        'Kiem tra phai dong tieu de ko
        If (dong = -1) Then
            Return
        End If

        'Dua du lieu tu DataGridView len cac textbox va combox
        txtSoHoSo.Text = dgvDSCongTrinh.Rows(dong).Cells("SoHoSo").Value.ToString()
        txtTenCongTrinh.Text = dgvDSCongTrinh.Rows(dong).Cells("TenCongTrinh").Value.ToString()

        dtNgayNhan.Text = String.Format("{0:dd/MM/yyyy}", dgvDSCongTrinh.Rows(dong).Cells("NgayNhan").Value.ToString())

        txtHoSoNam.Text = dgvDSCongTrinh.Rows(dong).Cells("HoSoNam").Value.ToString()
        txtThanhPhanHoSo.Text = dgvDSCongTrinh.Rows(dong).Cells("ThanhPhanHoSo").Value.ToString()
        cmbHuyen.Text = dgvDSCongTrinh.Rows(dong).Cells("T_Huyen").Value.ToString()
        cmbXa.Text = dgvDSCongTrinh.Rows(dong).Cells("TenXa").Value.ToString()
        cmbPhong.Text = dgvDSCongTrinh.Rows(dong).Cells("TenPhong").Value.ToString()
        cmbKe.Text = dgvDSCongTrinh.Rows(dong).Cells("TenKe").Value.ToString()
        cmbKho.Text = dgvDSCongTrinh.Rows(dong).Cells("TenKho").Value.ToString()
        cmbTinhTrang.Text = dgvDSCongTrinh.Rows(dong).Cells("TenTinhTrang").Value.ToString()
    End Sub

    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        Dim sql As String
        Dim tb As DataTable

        If txtSoHoSo.Text = "" Then
            Return
        End If
        '============ Kiểm tra có tồn tại khóa ngoại ko
        sql = "SELECT * FROM PHIEUMUON_HSDV WHERE SoHoSo = '" & txtSoHoSo.Text & "'"
        tb = LoadThongTin(sql)
        If (tb.Rows.Count > 0) Then
            MessageBox.Show("Hồ sơ đang mượn. Không được xoá", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Else
            sql = "DELETE FROM CONGTRINH WHERE SoHoSo = '" & txtSoHoSo.Text & "'"
            Dim t As Boolean
            t = ThaoTacDuLieu(sql)
            If t = True Then
                MessageBox.Show("Xóa công trình thành công", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                LuuThongTinXoa()
                LamTuoi()
                NapDSCongTrinh()

            End If
        End If

    End Sub

    Private Sub txtTenCongTrinh_KeyUp(sender As Object, e As KeyEventArgs) Handles txtTenCongTrinh.KeyUp
        NapDSCongTrinh()
    End Sub

    Private Sub dgvDSCongTrinh_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDSCongTrinh.CellContentClick

    End Sub

    Private Sub dgvDSCongTrinh_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvDSCongTrinh.RowPostPaint
        ' xuat so thu tu tu dong cho datagridview
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < dgvDSCongTrinh.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If dgvDSCongTrinh.RowHeadersWidth < Size.Width + 20 Then dgvDSCongTrinh.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
End Class